package com.myCinema.biz.servlet;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myCinema.dao.impl.MovieDaoImpl;
import com.myCinema.entity.Category;
import com.myCinema.entity.Movie;

public class SaveMovieServlet extends HttpServlet {


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");

		int id = 0;
		String title = request.getParameter("title");
		String movieCode = request.getParameter("movieCode");
		int categoryId = Integer.parseInt(request.getParameter("cid"));
		String director = request.getParameter("director");
		Date dateReleased = Date.valueOf(request.getParameter("date"));

		if(request.getParameter("id")!=null && !request.getParameter("id").isEmpty()){
			id= Integer.parseInt(request.getParameter("id")); 
			Movie m = new Movie(id, movieCode, 0, title, director,dateReleased);
			m.setCategory(new Category(categoryId,null));
			new MovieDaoImpl().update(m);			
			}else{
			Movie m = new Movie(id, movieCode, 0, title, director,dateReleased);
			m.setCategory(new Category(categoryId,null));
			new MovieDaoImpl().add(m);
		}
		System.out.println(title);
		System.out.println(movieCode);
		System.out.println(categoryId);
		System.out.println(director);
		System.out.println(dateReleased);
		System.out.println(request.getParameter("id"));
		response.sendRedirect("MovieListServlet");

	}

}
